package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.ComponentUnauthorizedMapper;
import com.yl.applicationsecurity.mapper.ScriptStateMapper;
import com.yl.applicationsecurity.pojo.ComponentUnauthorized;
import com.yl.applicationsecurity.pojo.InspectionConfiguration;
import com.yl.applicationsecurity.pojo.ScriptState;
import com.yl.applicationsecurity.service.ComponentUnauthorizedService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-01-22 17:47:35
 */
@Service
public class ComponentUnauthorizedServiceImpl  extends ServiceImpl<ComponentUnauthorizedMapper, ComponentUnauthorized> implements ComponentUnauthorizedService {

    @Autowired
    private ScriptStateMapper scriptStateMapper;

    @Override
    public PageInfo<ComponentUnauthorized> findListByComponentUnauthorized(String[] nationSelect, ComponentUnauthorized componentUnauthorized, Date startCreateDate, Date endCreateDate, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize );//每页显示8条
        LambdaQueryWrapper<ComponentUnauthorized> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( StringUtils.isNotEmpty(componentUnauthorized.getUrl())){
            lambdaQueryWrapper.like(ComponentUnauthorized::getUrl,componentUnauthorized.getUrl());
        }
        if ( componentUnauthorized.getFirstdiscovery()!=null){
            lambdaQueryWrapper.eq(ComponentUnauthorized::getFirstdiscovery,componentUnauthorized.getFirstdiscovery());
        }

        if ( componentUnauthorized.getStatus()!=null){
            lambdaQueryWrapper.eq(ComponentUnauthorized::getStatus,componentUnauthorized.getStatus());
        }

        if(startCreateDate!=null){
            lambdaQueryWrapper.ge(ComponentUnauthorized::getCreateDate, startCreateDate);
        }


        if(endCreateDate!=null){
            lambdaQueryWrapper.le(ComponentUnauthorized::getCreateDate, endCreateDate);
        }

        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(ComponentUnauthorized::getNation,nationSelect);
        }

        lambdaQueryWrapper.orderByDesc(ComponentUnauthorized::getCreateDate);//根据首次发现时间排序
        List<ComponentUnauthorized> list = baseMapper.selectList(lambdaQueryWrapper);


        PageInfo<ComponentUnauthorized> pageInfo = new PageInfo<ComponentUnauthorized>(list);


        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;
    }

    @Override
    public int updateStatus(String[] ids, Integer updateStatus) {
        LambdaUpdateWrapper<ComponentUnauthorized> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(ComponentUnauthorized::getStatus,updateStatus).in(ComponentUnauthorized::getId , ids);
        if(ids==null||ids.length==0||updateStatus==null){
            return 1;
        }
        return baseMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public int updateNation(String[] ids, String nation) {
        LambdaUpdateWrapper<ComponentUnauthorized> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(ComponentUnauthorized::getNation,nation).in(ComponentUnauthorized::getId , ids);
        if(ids==null||ids.length==0||nation==null){
            return 1;
        }
        return baseMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public int insert(ComponentUnauthorized componentUnauthorized) {
        componentUnauthorized.setId(UUID.randomUUID().toString());
        componentUnauthorized.setStatus(0);//状态修改为待处理
        componentUnauthorized.setCreateDate(new Date());//创建时间
        if(componentUnauthorized.getUrl()!=null&componentUnauthorized.getNation()!=null&componentUnauthorized.getFirstdiscovery()!=null){
            return baseMapper.insert(componentUnauthorized);
        }
        return 0;
    }
}

